//
//  MineTopView.swift
//  77Club
//
//  Created by Apple on 2024/9/4.
//

import UIKit

class MineNavView: UIView {

    private let backView = c_make_ex(UIImageView.self)
        .mode(.scaleToFill).image("Mine_topBg").end()
    private let settingButton = c_make_ex(UIButton.self)
        .image("Mine_setting").end()
    private let scanButton = c_make_ex(UIButton.self)
        .image("Mine_scan").end()
    private let bellButton = c_make_ex(UIButton.self)
        .image("Mine_bell").end()
    private let iconCircleView = c_make_ex(UIView.self)
        .bgColor(.clear).corner(28.0.Scale)
        .border("#E8E9EB", width: 2.0.Scale).end()
    private let shadowView = c_make_ex(UIView.self)
        .bgColor(.black.alpha(0.1)).end()
    private let iconImageView = c_make_ex(UIImageView.self)
        .mode(.scaleToFill).corner(24.0.Scale).end()
    private let namLabel = c_make_ex(UILabel.self)
        .color(.white).font(16.font_semiBold).end()
    private let idLabel = c_make_ex(UILabel.self)
        .color(.white).font(13.font_medium).end()
    
    var model: ClubMineUserModel? {
        didSet{
            guard let md = model else { return }
            iconImageView.loadImage(url: md.face)
            namLabel.text = md.user_name
            if md.vir_user_id != "0" {
                idLabel.text = "ID: " + md.vir_user_id
            }else{
                idLabel.text = "未登录"
            }
        }
    }
    
    var settingBlock: (() -> Void)?
    var scanBlock: (() -> Void)?
    var msgBlock: (() -> Void)?
    var userInfoBlock: (() -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            backView
            settingButton
            scanButton
            bellButton
            iconCircleView.subviews{
                iconImageView.subviews{
                    shadowView
                }
            }
            namLabel
            idLabel
        }
        
        backView.fillContainer()
        settingButton.trailing(12.0.Scale).top(.statusHeight + 8.0.Scale).size(32.0.Scale)
        scanButton.trailing(48.0.Scale).top(.statusHeight + 8.0.Scale).size(32.0.Scale)
        bellButton.trailing(84.0.Scale).top(.statusHeight + 8.0.Scale).size(32.0.Scale)
        iconCircleView.leading(16.0.Scale).bottom(20.0.Scale).size(56.0.Scale)
        iconImageView.size(48.0.Scale).centerVertically().centerHorizontally()
        shadowView.fillContainer()
        namLabel.leading(88.0.Scale).trailing(50.0.Scale)
        namLabel.CenterY == iconImageView.CenterY - 10
        idLabel.leading(88.0.Scale).trailing(50.0.Scale)
        idLabel.CenterY == iconImageView.CenterY + 10
        
        settingButton.addTarget(self, action: #selector(clickSettingButton), for: .touchUpInside)
        scanButton.addTarget(self, action: #selector(clickScanButton), for: .touchUpInside)
        bellButton.addTarget(self, action: #selector(clickMessageButton), for: .touchUpInside)
        let tapG = UITapGestureRecognizer(target: self, action: #selector(clickUserIcon))
        iconImageView.isUserInteractionEnabled = true
        iconCircleView.isUserInteractionEnabled = true
        iconImageView.addGestureRecognizer(tapG)
    }
    
    @objc private func clickSettingButton(){
        settingBlock?()
    }
    
    @objc private func clickScanButton(){
        scanBlock?()
    }
    
    @objc private func clickMessageButton(){
        msgBlock?()
    }
    
    @objc private func clickUserIcon(){
        userInfoBlock?()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


class MineTopView: UIView {

    private let backView = c_make_ex(UIView.self)
        .bgColor(.white).corner(12.0.Scale).end()
    private let signinButton = c_make_ex(UIImageView.self)
        .image("Mine_signin").mode(.scaleAspectFill).end()
    private let integralButton = c_make_ex(UIImageView.self)
        .image("Mine_integral").mode(.scaleAspectFill).end()
    private let integralLabel = c_make_ex(UILabel.self)
        .color("#E89A00").font(20.font_semiBold).text("999").end()
    private let signImageView = c_make_ex(UIImageView.self)
        .image("Mine_check").end()
    private var signLabel = c_make_ex(UILabel.self)
        .color(.white).font(16.font_semiBold)
        .text("今日未签到").end()
    
    var model: ClubMineUserModel? {
        didSet{
            guard let md = model else { return }
            if md.is_qiandao {
                signImageView.widthConstraint?.constant = 18
                signLabel.text = "已签到"
            }else{
                signImageView.widthConstraint?.constant = 0
                signLabel.text = "今日未签到"
            }
            integralLabel.text = md.point
        }
    }
    
    var signinBlock: (() -> Void)?
    var integralBlock: (() -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            backView.subviews{
                signinButton.subviews{
                    signImageView
                    signLabel
                }
                integralButton.subviews{
                    integralLabel
                }
            }
        }
        
        let width = (CGFloat.screenWidth - 60.0.Scale) / 2.0
        backView.leading(12.0.Scale).trailing(12.0.Scale).top(12.0.Scale).bottom(8.0.Scale)
        signinButton.leading(12.0.Scale).top(12.0.Scale).width(width).height(96.0.Scale)
        integralButton.trailing(12.0.Scale).top(12.0.Scale).width(width).height(96.0.Scale)
        signImageView.leading(12.0.Scale).height(18).bottom(9).width(0)
        signLabel.CenterY == signImageView.CenterY
        signLabel.Leading == signImageView.Trailing + 4
        integralLabel.leading(12.0.Scale).trailing(12.0.Scale).top(30.0.Scale)
        
        let signinTap = UITapGestureRecognizer(target: self, action: #selector(clickSigninButton))
        signinButton.isUserInteractionEnabled = true
        signinButton.addGestureRecognizer(signinTap)
        let integralTap = UITapGestureRecognizer(target: self, action: #selector(clickIntegralButton))
        integralButton.isUserInteractionEnabled = true
        integralButton.addGestureRecognizer(integralTap)
    }
    
    @objc private func clickSigninButton(){
        signinBlock?()
    }
    
    @objc private func clickIntegralButton(){
        integralBlock?()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


class MineGongNengView: UIView, UICollectionViewDelegate, UICollectionViewDataSource {

    private let backView = c_make_ex(UIView.self)
        .bgColor(.white).corner(12.0.Scale).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(14.font_semiBold)
        .text("常用功能").end()
    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    
    var sourceData: [ClubMineNavDataModel]?{
        didSet{
            collectionView.reloadData()
        }
    }
    
    var signinBlock: (() -> Void)?
    var pushBlock: ((_ model: ClubMineNavDataModel) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            backView.subviews{
                titleLabel
                collectionView
            }
        }
        
        backView.leading(12.0.Scale).trailing(12.0.Scale).top(8.0.Scale).bottom(8.0.Scale)
        titleLabel.leading(12.0.Scale).top(12.0.Scale)
        collectionView.leading(0).trailing(0).top(40.0.Scale).height(55.0.Scale)
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSize(width: (.screenWidth - 24.0)/4, height: 55.0.Scale)
//        layout.sectionInset = UIEdgeInsets(top: 0, left: 12.0.Scale, bottom: 0, right: 0)
        
        collectionView.collectionViewLayout = layout
        collectionView.backgroundColor = .clear
        collectionView.register(ClubMineCateCell.self, forCellWithReuseIdentifier: ClubMineCateCell.description())
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return sourceData?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ClubMineCateCell.description(), for: indexPath) as? ClubMineCateCell else { return UICollectionViewCell() }
        cell.model = sourceData?[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        guard let md = sourceData?[indexPath.row] else { return }
        pushBlock?(md)
    }
    
    @objc private func clickSettingButton(){
        signinBlock?()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


class MineGameView: UIView, UICollectionViewDelegate, UICollectionViewDataSource {

    private let titleLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(14.font_semiBold)
        .text("热门游戏").end()
    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    
    var sourceData: [ClubMineNavDataModel]?{
        didSet{
            collectionView.reloadData()
        }
    }
    
    var signinBlock: (() -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .clear
        
        subviews{
            titleLabel
            collectionView
        }
        
        titleLabel.leading(12.0.Scale).top(12.0.Scale)
        collectionView.leading(0).trailing(0).top(40.0.Scale).height(155.0.Scale)
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 16
        layout.minimumInteritemSpacing = 24
        layout.scrollDirection = .vertical
        layout.itemSize = CGSize(width: 48.0.Scale, height: 68.0.Scale)
        layout.sectionInset = UIEdgeInsets(top: 0, left: 12.0.Scale, bottom: 0, right: 0)
        
        collectionView.collectionViewLayout = layout
        collectionView.backgroundColor = .clear
        collectionView.register(MineGameItemCellView.self, forCellWithReuseIdentifier: MineGameItemCellView.description())
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return sourceData?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MineGameItemCellView.description(), for: indexPath) as? MineGameItemCellView else { return UICollectionViewCell() }
        cell.model = sourceData?[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let gid = sourceData?[indexPath.row].gid ?? ""
        let dict = ["gameId": gid]
        NotificationCenter.default.post(name: .mineToGameDetailVC, object: nil, userInfo: dict)
    }
    
    @objc private func clickSettingButton(){
        signinBlock?()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


//MARK: 游戏详情热门游戏cell
class MineGameItemCellView: UICollectionViewCell {
    
    private let iconImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(8.0.Scale).end()
    private let shadowView = c_make_ex(UIView.self)
        .bgColor(.black.alpha(0.1)).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(12.font_medium).alignment(.center).end()
    
    var model: ClubMineNavDataModel?{
        didSet{
            guard let md = model else { return }
            nameLabel.text = md.name
            iconImageView.loadImage(url: md.icon)
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        
        contentView.subviews{
            iconImageView.subviews{
                shadowView
            }
            nameLabel
        }
        
        iconImageView.top(0).size(48.0.Scale).centerHorizontally()
        shadowView.fillContainer()
        nameLabel.leading(0).trailing(0).centerHorizontally()
        nameLabel.Top == iconImageView.Bottom + 5.0.Scale
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


//MARK: 图文分类cell
class ClubMineCateCell: UICollectionViewCell {
    
    private let iconImageView = c_make_ex(UIImageView.self).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(12.font_semiBold).alignment(.center).end()
    
    var model: ClubMineNavDataModel?{
        didSet{
            guard let md = model else { return }
            nameLabel.text = md.name
            iconImageView.loadImage(url: md.icon)
        }
    }
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        
        contentView.subviews{
            iconImageView
            nameLabel
        }
        
        iconImageView.top(0).size(32.0.Scale).centerHorizontally()
        nameLabel.leading(0).trailing(0).bottom(0).centerHorizontally()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
